import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = 'afterSales/api/v1';
import * as dto from './DTO';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询服务贸易方式'
});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增服务贸易方式'
});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑服务贸易方式'
});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废服务贸易方式'
});

// 查询
export const getList = bodyCondition => {
    const queryString = stringify(bodyCondition);
    return request(`${baseUrl}/tradeMethods?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id => request(`${baseUrl}/tradeMethods/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));

// 新增
export const addData = data => request(`${baseUrl}/tradeMethods`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateData = (id, data) => request(`${baseUrl}/tradeMethods/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
// 作废
export const abandonData = (id, data) => request(`${baseUrl}/tradeMethods/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
